package cn.huanba.service.impl;

import cn.huanba.dao.DockDao;
import cn.huanba.dao.ProductDao;
import cn.huanba.entity.Dock;
import cn.huanba.entity.Product;
import cn.huanba.service.ProductService;
import cn.huanba.utils.OssUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Product)表服务实现类
 *
 * @author makejava
 * @since 2021-01-07 17:23:27
 */
@Service("productService")
public class ProductServiceImpl implements ProductService {
    @Resource
    private ProductDao productDao;
    @Autowired
    private OssUtils ossUtils;
    @Autowired
    private DockDao dockDao;

    /**
     * 替换图片路径为阿里的oss
     *
     * @param product
     */
    public void setPic(Product product) {
        product.getPictureList().stream().forEach(e -> e.setPath(ossUtils.createFileName(e.getPath())));
    }


    @Override
    public List<Product> getProductAndDock(Integer uid) {
        return productDao.getProductAndDock(uid);
    }

    @Override
    public int saveDock(Dock dock) {
        return dockDao.insert(dock);
    }

    @Override
    public List<Product> getProductListByTid(Integer tid) {
        Product product = new Product();
        product.setTid(tid);
        List<Product> products = productDao.queryAll(product);
        products.stream().forEach(e -> setPic(e));
        return products;
    }

    @Override
    public List<Product> getProductListByUser(Integer uid) {
        Product product = new Product();
        product.setUid(uid);
        List<Product> products = productDao.queryAll(product);
        products.stream().forEach(e -> setPic(e));
        return products;
    }

    @Override
    public List<Product> queryAllByproductName(String productName) {
        Product product = new Product();
        product.setProductname(productName);
        List<Product> products = productDao.queryAll(product);
        products.stream().forEach(e -> setPic(e));
        return products;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param pid 主键
     * @return 实例对象
     */
    @Override
    public Product queryById(Integer pid) {
        Product product = this.productDao.queryById(pid);
        setPic(product);
        return product;
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<Product> queryAllByLimit(int offset, int limit) {
        return this.productDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param product 实例对象
     * @return 实例对象
     */
    @Override
    public int insert(Product product) {

        return this.productDao.insert(product);
    }

    /**
     * 修改数据
     *
     * @param product 实例对象
     * @return 实例对象
     */
    @Override
    public Product update(Product product) {
        this.productDao.update(product);
        return this.queryById(product.getPid());
    }

    /**
     * 通过主键删除数据
     *
     * @param pid 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer pid) {
        return this.productDao.deleteById(pid) > 0;
    }
}